package com.honey.mybatis.generator.mappper;

import com.honey.compilation.xml.XmlElement;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Schema;
import com.honey.mybatis.generator.MybatisPlugin;

public class Exists extends AbstractMybatisMapper{

	public Exists(MybatisPlugin belongPlugin) {
		super(belongPlugin);
	}

	public void appendElement(XmlElement parent, Schema schema, IntrospectSchemaColumn introspectSchemaColumn ){
		parent.addNewLine();
		parent.addComments(
				"使用数据库的exists函数判断查询条件是否有记录集"
		);
		
		XmlElement select = new XmlElement(parent,"select")
		.addAttribute("id",MapperConstant.EXISTS_MAPPER_ID)
		.addAttribute("parameterType",super.getModelPrimitiveClassType(schema).getLongName())
		.addAttribute("resultType", "java.lang.Boolean")
		.addAttribute("timeout", mybatisPlugin.getConnectionTimeOut());
		select.addTextElement("SELECT EXISTS( SELECT 1 FROM "+ schema.getName());
		addDefaultDynamicCondition(select,introspectSchemaColumn.allColumns());
		select.addTextElement(") AS boolean ;");
	}
}
